Skip to content

Commit d23e791

Browse files
authored
enhance(lint): fix common errors (#26948)
1 parent d3adffb commit d23e791

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

lint/fix.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import chalk from 'chalk-template';
1414
import dataFolders from '../scripts/lib/data-folders.js';
1515

1616
import fixBrowserOrder from './fixer/browser-order.js';
17+
import fixCommonErrors from './fixer/common-errors.js';
1718
import fixFeatureOrder from './fixer/feature-order.js';
1819
import fixPropertyOrder from './fixer/property-order.js';
1920
import fixStatementOrder from './fixer/statement-order.js';
@@ -30,6 +31,7 @@ const dirname = fileURLToPath(new URL('.', import.meta.url));
3031

3132
const FIXES = Object.freeze({
3233
descriptions: fixDescriptions,
34+
common_errors: fixCommonErrors,
3335
flags: fixFlags,
3436
links: fixLinks,
3537
mdn_urls: fixMDNURLs,

lint/fixer/common-errors.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* This file is a part of @mdn/browser-compat-data
2+
* See LICENSE file for more information. */
3+
4+
import assert from 'node:assert/strict';
5+
6+
import { fixCommonErrorsInCompatStatement } from './common-errors.js';
7+
8+
const tests: { input: any; output?: any }[] = [
9+
// Replace unwrapped "false".
10+
{
11+
input: false,
12+
output: {
13+
version_added: false,
14+
},
15+
},
16+
// Replace wrapped "mirror".
17+
{
18+
input: {
19+
version_added: 'mirror',
20+
},
21+
output: 'mirror',
22+
},
23+
];
24+
25+
describe('fix -> common errors', () => {
26+
let i = 1;
27+
for (const test of tests) {
28+
it(`Test #${i}`, () => {
29+
const input = {
30+
support: {
31+
firefox_android: test.input,
32+
},
33+
};
34+
const output = {
35+
support: {
36+
firefox_android: test.output ?? test.input,
37+
},
38+
};
39+
40+
fixCommonErrorsInCompatStatement(input);
41+
42+
assert.deepStrictEqual(input, output);
43+
});
44+
45+
i += 1;
46+
}
47+
});

lint/fixer/common-errors.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* This file is a part of @mdn/browser-compat-data
2+
* See LICENSE file for more information. */
3+
4+
import fs from 'node:fs';
5+
6+
import { CompatStatement, SimpleSupportStatement } from '../../types/types.js';
7+
import { walk } from '../../utils/index.js';
8+
9+
/**
10+
* Fixes common errors in CompatStatements.
11+
*
12+
* - Replaces `browser: { version_added: "mirror" }` with `browser: "mirror"`
13+
* - Wraps `browser: false` with `browser: `{ version_added: false }`
14+
*
15+
* @param compat The compat statement to fix
16+
*/
17+
export const fixCommonErrorsInCompatStatement = (
18+
compat: CompatStatement,
19+
): void => {
20+
for (const browser of Object.keys(compat.support)) {
21+
if (compat.support[browser] === false) {
22+
compat.support[browser] = {
23+
version_added: false,
24+
} satisfies SimpleSupportStatement;
25+
} else if (
26+
typeof compat.support[browser] === 'object' &&
27+
JSON.stringify(compat.support[browser]) === '{"version_added":"mirror"}'
28+
) {
29+
compat.support[browser] = 'mirror';
30+
}
31+
}
32+
};
33+
34+
/**
35+
* Update compat data to 'mirror' if the statement matches mirroring
36+
* @param filename The name of the file to fix
37+
*/
38+
const fixCommonErrors = (filename: string): void => {
39+
if (filename.includes('/browsers/')) {
40+
return;
41+
}
42+
43+
const actual = fs.readFileSync(filename, 'utf-8').trim();
44+
const bcd = JSON.parse(actual);
45+
for (const { compat } of walk(undefined, bcd)) {
46+
fixCommonErrorsInCompatStatement(compat);
47+
}
48+
const expected = JSON.stringify(bcd, null, 2);
49+
50+
if (actual !== expected) {
51+
fs.writeFileSync(filename, expected + '\n', 'utf-8');
52+
}
53+
};
54+
55+
export default fixCommonErrors;

0 commit comments

Comments
 (0)